﻿using MediatR;
using RBAC.ApiWrites.Applocation.Command;
using RBAC.Domain.RbacEntity;
using RBAC.Infrasturcture.Interfaces;
using RBAC.ResultCode;

namespace RBAC.ApiWrites.Applocation.Commandhandler
{
    public class DeleteRoleCommandhandler : IRequestHandler<DeleteRoleCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<RolePermission> rolepemissionrepository;
        private readonly IBaseRepository<Role> rolerepository;

        public DeleteRoleCommandhandler(IBaseRepository<RolePermission> rolepemissionrepository,IBaseRepository<Role> rolerepository)
        {
            this.rolepemissionrepository = rolepemissionrepository;
            this.rolerepository = rolerepository;
        }
        public async Task<ApiResult<int>> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
        {
            var role = rolerepository.GetAll().FirstOrDefault(x => x.RoleId == request.RoleId);
         var res=   await rolerepository.Delete(role);

            var Rolelist = rolepemissionrepository.GetAll().Where(x => x.RoleId == request.RoleId).ToList();
            await rolepemissionrepository.DeleteRange(Rolelist);
          





            return new ApiResult<int>()
            {
                Code = ApiResultEnum.删除成功
                ,
                Message = ApiResultEnum.删除成功.ToString(),
                Data = res
            };
        }
    }
}
